package com.example.demo.controller;

import java.util.Map;

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

/**
 * @author m
 * @date 2024-06-16
 * @Descripion: 自定义异常数据返回
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
	@Override
	public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
		Map<String, Object> map = super.getErrorAttributes(webRequest, options);

		if ((Integer) map.get("status") == 500) {
			map.put("path", map.get("path"));
			map.put("error", map.get("error"));
			map.put("message", "内部异常");
			map.put("status", map.get("status"));
		} else if ((Integer) map.get("status") == 404) {
			map.put("path", map.get("path"));
			map.put("error", map.get("error"));
			map.put("message", "页面找不到");
			map.put("status", map.get("status"));
		}
		return map;
	}
}